<?php
/**
 * GeryEngine
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this release in the file LICENSE
 *
 * @copyright  Copyright (c) 2007 Arjen Brouwer
 * @license    New BSD License
 * @author     Arjen Brouwer
 */

if ( defined('ZOE_BASE_DIR')) 
	return;

class Properties {
	
	private $properties;
	private $initialized = false;
	
	public function __construct() {
		$this->properties = array();
	}
	
	public function combine(Properties $properties) 
	{
		// merge the supplied properties with this properties
		foreach ($properties->getPropertiesArray() as $key => $value) 
		{
			$this->properties[$key] = $value;
		}
	}
	
	/**
	 * @return boolean
	 */
	public function isInitialized() {
		return $this->initialized;
	}
	
	public function loadProperties($file) 
	{
		
		if (! file_exists($file) || ! is_file($file))
			throw new Exception("Could not read properties file '{$file}'");
		
		
		$props = parse_ini_file($file, false);
		
		// merge properties
		foreach ($props as $key => $value)
			$this->properties[$key] = $value;
			
		$this->initialized = true;
	}
	
	/**
	 * Get a subset of this property set
	 *
	 * @return Properties
	 */
	public function subset($subKey) {
		
		$subKey .= '.';
		$subKeyLength = strlen($subKey);
		
		$subSet = new Properties();
		
		foreach ($this->properties as $key => $value) 
		{
			if (strpos($key, $subKey) === 0) 
			{
				$subSet->setProperty(substr($key, $subKeyLength), $value);
			}
		}
		
		
		return $subSet;
	}
	
	public function getKeys() {
		return array_keys($this->properties);
	}
	
	public function addProperty($prop, $value) {
		
		if (key_exists($prop, $this->properties)) {
			if (is_array($this->properties[$prop])) {
				$this->properties[$prop][] = $value;
			} else {
				$oldValue = $this->properties[$prop];
				$this->properties[$prop] = array($val, $oldValue);
			}
				
		} else {
			$this->setProperty($prop, $value);
		}
	}
	
	public function setProperty($prop, $value) {
		$this->properties[$prop] = $value;
		
		$this->initialized = true;
	}
	
	public function clearProperty($prop) {
		unset($this->properties[$prop]);
	}
	
	/**
	 * @return string Returns a string
	 */
	public function getProperty($prop, $default = null) {
		if (key_exists($prop, $this->properties)) {
			return $this->properties[$prop];
		} else {
			return $default;
		}
	}
	
	/**
	 * Get a property as a string
	 * 
	 * @param string $key
	 * @return string
	 */
	public function getString($key) {
		return (string)$this->getProperty($key);
	}
	
	public function getArray($prop) {
		$value = $this->getProperty($prop);
		return preg_split('/\s*,\s*/', $value, -1, PREG_SPLIT_NO_EMPTY);
	}
	
	public function getBoolean($prop) {
		return (bool)$this->getProperty($prop);
	}
	
	public function getInteger($prop) {
		return (int)$this->getProperty($prop);
	}

	public function getPropertiesArray() {
		return $this->properties;
	}
	
	/**
	 * Clear all properties
	 */
	public function clear() {
		$this->properties = array();
	}
}
?>